package service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.IntegralDao;
import dao.UserDao;
import dao.impl.IntegralDaoImpl;
import dao.impl.UserDaoImpl;
import domian.Integral;
import domian.Result;
import domian.ResultStatus;
import domian.User;
import redis.clients.jedis.Jedis;
import service.UserService;

import javax.servlet.http.HttpServletRequest;

public class UserServiceImpl implements UserService {

    /**
     * 登录
     * @param tel
     * @param password
     * @return
     */
    @Override
    public Result userLogin(String tel, String password) {
        UserDao ud = new UserDaoImpl();
        User login = ud.login(tel);
        ResultStatus rts = null;
        Result result = new Result();
        if (login == null){
            //登录失败用户不存在
            rts = ResultStatus.LOGIN_ERROR_USERNAME;
        }else {
            if (password.equals(login.getPassword())){
                //登录成功
                rts = ResultStatus.LOGIN_SUCCESS;
                result.setObj(login);
            }else {
                //登录失败，密码错误
                rts = ResultStatus.LOGIN_ERROR_PASSWORD;
            }
        }

        result.setCode(rts.getCode());
        result.setMsg(rts.getMsg());

        return result;
    }

    @Override
    public User getUser(String tel, String password){
        UserDao ud = new UserDaoImpl();
        User user = ud.login(tel);
        if (password.equals(user.getPassword())) {
            return user;
        }
        return user;
    }

    /**
     * 注册
     * @param tel
     * @param password
     * @param email
     * @return
     */
    @Override
    public Result userRegister(String tel, String password, String email) {
        Result result = new Result();
        //记录注册状态
        ResultStatus rts = null;
        //用户数据访问
        UserDao ud = new UserDaoImpl();
        User login = ud.login(tel);
        if (login == null){
            User user = new User(tel, password, email,10);
            int addUser = ud.addUser(user);
            if (addUser > 0){
                //注册成功
                rts = ResultStatus.REGISTER_SUCCESS;
                //积分变动表对象
                Integral integral = new Integral();
                //获取到用户然后使用其id
                User login1 = ud.login(tel);
                //将id放入返回前端的对象，让前端使用
                result.setObj(login1.getUid());
                //积分变动的用户id
                integral.setUid(login1.getUid());
                //积分追加10分
                integral.setIntegral(10);
                //积分变动原因
                integral.setCause(ResultStatus.INTEGRAL_CHANGE.getMsg());
                IntegralDao ird = new IntegralDaoImpl();
                //将积分变动原因存入积分变动表
                ird.addIntegral(integral);
            }else{
                //注册失败
                rts = ResultStatus.REGISTER_ERROR;
            }
        }else {
            //注册失败，用户名已存在
            rts = ResultStatus.REGISTER_ERROR;
        }
        result.setCode(rts.getCode());
        result.setMsg(rts.getMsg());

        return result;
    }

    @Override
    public User findUserById(Integer uid) {
        UserDao userDao = new UserDaoImpl();
        return userDao.findUserById(uid);
    }

    @Override
    public void userUpdate(User user, String redisKey) throws JsonProcessingException {
        Jedis jedis = new Jedis();
        //获取到当前redis存活时间
        long tl = jedis.ttl(redisKey);
        int ttl = (int) tl;

        ObjectMapper om = new ObjectMapper();
        //将修改后的数据写入redis
        jedis.set(redisKey,om.writeValueAsString(user));
        //并将剩余存活时间集成过去
        jedis.expire(redisKey,ttl);
    }

}
